﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace PTCScheduler.FileMenu_Pages
{
    /// <summary>
    /// Interaction logic for Add_StudentPage.xaml
    /// </summary>
    public partial class Add_StudentPage : Page
    {
        public Add_StudentPage()
        {
            InitializeComponent();
        }

        //Method to clear the textboxes within student form.
        public void ClearForm()
        {
            //Clear user interface textboxes.
            txtStudentFName.Clear();
            txtStudentLName.Clear();
            txtStudentID.Clear();
            txtParentEmail.Clear();
            txtParentPhone.Clear();

            //Refocus to student first name.
            txtStudentFName.Focus();
        }

        //Method to check for valid email.
        public Boolean validEmail(String email)
        {
            if (email.Contains("@") && email.Contains("."))
            {
                return true;
            }
            return false;
        }

        //Handles the submit button click event.
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            //Hide all labels by default.
            HideAllLabels();
            //Try catch to catch format exception.
            try{
                //If student name is empty throw format exception.
                if (txtStudentFName.Text == String.Empty || txtStudentLName.Text == String.Empty || txtStudentID.Text == String.Empty)
                {
                    throw new MissingFieldException();
                }
                if (!Regex.IsMatch(txtStudentFName.Text, @"^[a-zA-Z]+$") || !Regex.IsMatch(txtStudentLName.Text, @"^[a-zA-Z]+$") || Regex.IsMatch(txtStudentID.Text, @"^[a-zA-Z]+$"))
                {
                    throw new FormatException();
                }
                if (!String.IsNullOrWhiteSpace(txtParentEmail.Text))
                {
                    if (!validEmail(txtParentEmail.Text))
                    {
                        throw new ArgumentException();
                    }
                }

                //Create new student object.
                Student student = new Student(txtStudentFName.Text, txtStudentLName.Text,
                         Convert.ToInt32(txtStudentID.Text), txtParentEmail.Text, txtParentPhone.Text);

                //Logic to handle inserting student.
                try
                {
                    //Add new student to database. (Need Try Catch)
                    student.AddNewStudent();

                    //Update and fill datagrid with new student information.
                    dataGridStudent.ItemsSource = student.dvStudent();

                    //Prompt user that student was added successfully.
                    lblStudentSuccess.Visibility = Visibility.Visible;

                    //Clear the form for new student.
                    ClearForm();
                }
                catch (Exception ex)
                {
                    //Display the student exists label.
                    lblStudentExists.Visibility = Visibility.Visible;

                    //Display pre-existing student information.
                    dataGridStudent.ItemsSource = student.dvStudent();
                }//End Try-Catch
            }//handle Format exception
            catch (Exception ex)
            {
                if (ex is MissingFieldException)
                {
                    //Display the invalid information label.
                    lblInformationCheck.Visibility = Visibility.Visible;
                }
                else if (ex is FormatException)
                {
                    //Display please enter valid student id message.
                    lblValid.Visibility = Visibility.Visible;
                }
                else if (ex is ArgumentException)
                {
                    lblValidEmail.Visibility = Visibility.Visible;
                }
            }//End First Try-Catch
          }//End button submit

        //Method to hide all labels.
        private void HideAllLabels()
        {
            //Change visibility of all labels to hidden.
            lblInformationCheck.Visibility = Visibility.Hidden;
            lblStudentExists.Visibility = Visibility.Hidden;
            lblStudentSuccess.Visibility = Visibility.Hidden;
            lblValid.Visibility = Visibility.Hidden;
            lblValidEmail.Visibility = Visibility.Hidden;

            //Clear datagrid.
            dataGridStudent.ItemsSource = null;
        }
    }
}
